﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="Dept", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(Dept))]
    public class DeptSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IDept clazz;
        
        public DeptSerializer(IDept clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            if (((clazz.ObligorDescription == null) 
                        || (clazz.ObligorDescription.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "ObligorDescription");
            }
            for (int x = 0; ((clazz.ObligorDescription != null) 
                        && (x < clazz.ObligorDescription.Count)); x = (x + 1)) {
                Writer.WriteStartElement("ObligorDescription");
                if (clazz.ObligorDescription[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaObligorDescription = this.GetMetaData(clazz.ObligorDescription[x]);
                    Writer.WriteTypeAttribute(metaObligorDescription.TypeQualifiedName.Namespace, metaObligorDescription.TypeQualifiedName.Name);
                    metaObligorDescription.NewSerializer(clazz.ObligorDescription[x]).WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ObligorDescription");
                }
                Writer.WriteEndElement();
            }
            Writer.WriteStartElement("DeptTotal");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "decimal");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDecimal(((decimal)(clazz.DeptTotal))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("Solidarity");
            if (clazz.Solidarity!=null) {
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "Solidarity");
                Writer.WriteString(clazz.Solidarity.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Solidarity");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            clazz.ObligorDescription = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorDescription>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "ObligorDescription")); ) {
                if (Reader.LocalName=="ObligorDescription") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>Dept>ObligorDescription");
                    }
                    global::Xtee.Core.SerializationMetadata metaObligorDescription = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorDescription>(Reader, "ObligorDescription", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorDescription item = metaObligorDescription.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorDescription>();
                    clazz.ObligorDescription.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("ObligorDescription");
                        metaObligorDescription.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>Dept>ObligorDescription", Reader.LocalName);
                }
            }
            if ((clazz.ObligorDescription.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "ObligorDescription");
            }
            if (Reader.LocalName=="DeptTotal") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>Dept>DeptTotal");
                }
                clazz.DeptTotal = new global::Xtee.Core.Schema.XsdDecimal(Reader.ReadElementString("DeptTotal")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>Dept>DeptTotal", Reader.LocalName);
            }
            if (Reader.LocalName=="Solidarity") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>Dept>Solidarity");
                }
                clazz.Solidarity = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.Solidarity(Reader.ReadElementString("Solidarity"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>Dept>Solidarity", Reader.LocalName);
            }
        }
    }
}
